package com.shanwen.shopping.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.shanwen.shopping.R;

/**
 * material design风格的common dialog
 *
 * @author 刘宾
 * @date 2015年12月13日 上午10:20:44
 */
public class CommonDialog
        extends Dialog
        implements View.OnClickListener {
    private Context context;
    private Button confirmBtn;
    private Button cancelBtn;
    private TextView titleTv;
    private TextView contentTv;

    private String title;
    private String content;
    private String confirm;
    private String cancel;
    private OnClickListener listener;
    private boolean isCanceled = true;

    public CommonDialog(Context context) {
        super(context, R.style.CommonDialog_Dialog);
        this.context = context;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public CommonDialog setConfirmText(String confirm) {
        this.confirm = confirm;
        return this;
    }

    public CommonDialog setCancelText(String cancel) {
        this.cancel = cancel;
        return this;
    }

    public CommonDialog setOnClickListener(OnClickListener listener) {
        this.listener = listener;
        return this;
    }

    public CommonDialog setCanceledOnTouch(boolean isCanceled) {
        this.isCanceled = isCanceled;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(context).inflate(R.layout.common_dialog, null);
        setCancelable(isCanceled);
        setContentView(view);
        initView(view);
        initData();
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }

    private void initView(View view) {
        titleTv = (TextView) view.findViewById(R.id.common_dialog_title);
        contentTv = (TextView) view.findViewById(R.id.common_dialog_content);
        confirmBtn = (Button) view.findViewById(R.id.common_dialog_confirm);
        cancelBtn = (Button) view.findViewById(R.id.common_dialog_cancel);
        confirmBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
    }

    private void initData() {
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
        }
        if (!TextUtils.isEmpty(content)) {
            contentTv.setText(content);
        }
        if (!TextUtils.isEmpty(confirm)) {
            confirmBtn.setText(confirm);
        }
        if (!TextUtils.isEmpty(cancel)) {
            cancelBtn.setText(cancel);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_dialog_cancel:
                if (null != listener) {
                    listener.cancel();
                }
                this.dismiss();
                break;
            case R.id.common_dialog_confirm:
                if (null != listener) {
                    listener.confirm();
                }
                this.dismiss();
                break;
        }
    }

    public static interface OnClickListener {
        void confirm();

        void cancel();
    }
}
